/**
 * Copyright (c) 2018 开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.modules.yckj.common;

import io.renren.common.utils.DateUtils;
import io.renren.modules.yckj.dto.AppointDTO;
import io.renren.modules.yckj.dto.OrderDTO;
import io.renren.modules.yckj.dto.TimeDTO;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 日期处理
 *
 * @author Mark sunlightcs@gmail.com
 */
public class DateTimeUtils {

    public final static List<TimeDTO> times = List.of(
            new TimeDTO("00:00", "00:00~00:30", 2), new TimeDTO("00:30", "00:30~01:00", 2),
            new TimeDTO("01:00", "01:00~01:30", 2), new TimeDTO("01:30", "01:30~02:00", 2),
            new TimeDTO("02:00", "02:00~02:30", 2), new TimeDTO("02:30", "02:30~03:00", 2),
            new TimeDTO("03:00", "03:00~03:30", 2), new TimeDTO("03:30", "03:30~04:00", 2),
            new TimeDTO("04:00", "04:00~04:30", 2), new TimeDTO("04:30", "04:30~05:00", 2),
            new TimeDTO("05:00", "05:00~05:30", 2), new TimeDTO("05:30", "05:30~06:00", 2),
            new TimeDTO("06:00", "06:00~06:30", 2), new TimeDTO("06:30", "06:30~07:00", 2),
            new TimeDTO("07:00", "07:00~07:30", 2), new TimeDTO("07:30", "07:30~08:00", 2),
            new TimeDTO("08:00", "08:00~08:30", 2), new TimeDTO("08:30", "08:30~09:00", 2),
            new TimeDTO("09:00", "09:00~09:30", 2), new TimeDTO("09:30", "09:30~10:00", 2),
            new TimeDTO("10:00", "10:00~10:30", 2), new TimeDTO("10:30", "10:30~11:00", 2),
            new TimeDTO("11:00", "11:00~11:30", 2), new TimeDTO("11:30", "11:30~12:00", 2),
            new TimeDTO("12:00", "12:00~12:30", 2), new TimeDTO("12:30", "12:30~13:00", 2),
            new TimeDTO("13:00", "13:00~13:30", 2), new TimeDTO("13:30", "13:30~14:00", 2),
            new TimeDTO("14:00", "14:00~14:30", 2), new TimeDTO("14:30", "14:30~15:00", 2),
            new TimeDTO("15:00", "15:00~15:30", 2), new TimeDTO("15:30", "15:30~16:00", 2),
            new TimeDTO("16:00", "16:00~16:30", 2), new TimeDTO("16:30", "16:30~17:00", 2),
            new TimeDTO("17:00", "17:00~17:30", 2), new TimeDTO("17:30", "17:30~18:00", 2),
            new TimeDTO("18:00", "18:00~18:30", 2), new TimeDTO("18:30", "18:30~19:00", 2),
            new TimeDTO("19:00", "19:00~19:30", 2), new TimeDTO("19:30", "19:30~20:00", 2),
            new TimeDTO("20:00", "20:00~20:30", 2), new TimeDTO("20:30", "20:30~21:00", 2),
            new TimeDTO("21:00", "21:00~21:30", 2), new TimeDTO("21:30", "21:30~22:00", 2),
            new TimeDTO("22:00", "22:00~22:30", 2), new TimeDTO("22:30", "22:30~23:00", 2),
            new TimeDTO("23:00", "23:00~23:30", 2), new TimeDTO("23:30", "23:30~24:00", 2));

    /**
     * "状态，过期的2；已经预约1;可用0")
     *
     * @return 时间列表
     */
    public static List<TimeDTO> getTimeList(String dateTime, List<OrderDTO> orderDTOS) {
        Date dateNow = new Date();
        times.forEach(timeDTO -> {
            String time = timeDTO.getTime();
            AtomicInteger status = new AtomicInteger();
            //如果是当天
            Date chooseDate = DateUtils.parse(dateTime, "yyyy-MM-dd");
            int timeCurrent = Integer.parseInt(time.replace(":", ""));
            assert chooseDate != null;
            if (chooseDate.compareTo(dateNow) < 0) {
                //获取时间的前一部分
                int timeNow = Integer.parseInt(DateUtils.format(dateNow, DateUtils.TIME_PATTERN).substring(0, 5).replace(":", ""));
                if (timeCurrent < timeNow) {
                    status.set(2);
                }
                orderDTOS.forEach(orderDTO -> {
                    if (orderDTO.getBookAllTimes().contains(dateTime + " " + time)) {
                        status.set(1);
                    }
                });
            } else {
                orderDTOS.forEach(orderDTO -> {
                    if (orderDTO.getBookAllTimes().contains(dateTime + " " + time)) {
                        status.set(1);
                    }
                });
            }

            timeDTO.setStatus(status.get());
        });
        return times;
    }

    /**
     * "状态，过期的2；已经预约1;可用0")
     *
     * @return 时间列表
     */
    public static List<AppointDTO> getAppointList(List<OrderDTO> orderDTOS) {
        List<AppointDTO> appointDTOS = new ArrayList<>();
        List<String> keyList = new ArrayList<>();
        orderDTOS.forEach(orderDTO -> {
            if (orderDTO.getBookAllTimes() != null && !orderDTO.getBookAllTimes().isEmpty()) {
                Arrays.stream(orderDTO.getBookAllTimes().split(","))
                        .forEach(dt -> {
                            Date parse = DateUtils.parse(dt, DateUtils.DATE_TIME_PATTERN_16);
                            String key = DateUtils.format(parse, DateUtils.DATE_PATTERN);
                            if (!keyList.contains(key)) {
                                keyList.add(key);
                            }
                        });
            }
        });
        keyList.forEach(key -> {
            AppointDTO appointDTO = new AppointDTO();
            appointDTO.setTime(key);
            List<OrderDTO> orderDTOList = new ArrayList<>();
            orderDTOS.forEach(orderDTO -> {
                if (orderDTO.getBookAllTimes() != null && !orderDTO.getBookAllTimes().isEmpty()) {
                    Arrays.stream(orderDTO.getBookAllTimes().split(","))
                            .forEach(dt -> {
                                Date parse = DateUtils.parse(dt, DateUtils.DATE_TIME_PATTERN_16);
                                String dateKey = DateUtils.format(parse, DateUtils.DATE_PATTERN);
                                assert dateKey != null;
                                if (dateKey.equals(key)) {
                                    orderDTOList.add(orderDTO);
                                }
                            });
                }
            });
            appointDTO.setOrderDTOList(orderDTOList);
            appointDTO.setCount(orderDTOList.size());
            appointDTOS.add(appointDTO);
        });
        return appointDTOS;
    }

}
